package jeecg.business.job;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jeecg.business.entity.chat.ChatInfoEntity;
import jeecg.business.service.chat.ChatInfoServiceI;

import org.jeecgframework.core.util.DataUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ChatTask {
	@Autowired
	ChatInfoServiceI chatService;
	
	public static String QQ_CHAT_FILE = ResourceUtil.getSessionattachmenttitle("qq.file");
	
	/**
	 * 
	 * <p>
	 * 方法简要描述: 加载QQ信息
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version 
	 * @see
	 */
	@Scheduled(cron="0 45 0/1 * * ?")
//	@Scheduled(cron="45 0/1 * * * ?")
	public void loadQQChat(){
		File file = null;
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader reader = null;
		try{
			file = new File(QQ_CHAT_FILE);
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis,"utf-8");
			reader = new BufferedReader(isr);
			String line = null;
			String preLine = null;	//上一行
			String info = "";
			String qqName = null;	//QQ群名称
			ChatInfoEntity chat = null;	//进行保存的
			ChatInfoEntity temp = null;	//临时的
			int batchNum = 10000;	//批量次数
			Date limittime = DataUtils.getAddMonth(-3);
			List<ChatInfoEntity> list = new ArrayList<ChatInfoEntity>(batchNum);
			while ((line = reader.readLine()) != null) {
				if(StringUtil.isEmpty(line)) continue;
				
				
				
				if(line.startsWith("消息对象:") && preLine.equals("================================================================")){
					qqName = line.replace("消息对象:", "");
				}
				
				
				if("================================================================".equals(line) && chat != null){		//结束符
					if(StringUtil.isNotEmpty(info)){
						chat.setInfo(info);
						if(chat.getTime().getTime() >= limittime.getTime())
							list.add(chat);
						info = "";
						chat = null;
					}
				}
				
				preLine = line;
				
				
				
				temp = isQQPeopleInfo(line);
				if(temp != null){
					
					if(chat != null && StringUtil.isNotEmpty(info)){
						chat.setInfo(info);
						if(chat.getTime().getTime() >= limittime.getTime())
							list.add(chat);
						chat = null;	//重新置空
						info = "";
					}
					
					chat = temp.clone();
					chat.setQqGroupName(qqName);
					

				}else if(chat != null){
					info += line+" ";
				}
				
			}
			System.out.println("总条数："+list.size());
			if(list.size()>0)
				chatService.save(list);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(reader != null)
					reader.close();
				if(isr != null)
					isr.close();
				if(fis != null)
					fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	/**
	 * 
	 * <p>
	 * 方法简要描述: 判断是否是QQ的个人记录
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version 
	 * @see
	 * @param line
	 * @return
	 */
	ChatInfoEntity isQQPeopleInfo(String line){
		String [] p = line.split(" ");
		String qqnum = null;
		ChatInfoEntity chat = null;
		if(p!=null && p.length>=3 && line.endsWith(")")){
			chat = new ChatInfoEntity();
			String gettime = p[0]+" "+p[1];
			/**
			 * 判断是否是时间格式
			 */
			if(DataUtils.isTime(gettime, "yyyy-MM-dd HH:mm:ss")){
				//提取QQ号
				int pos = line.lastIndexOf("(");
				qqnum = line.substring(pos+1,line.length()-1);
				if(StringUtil.isNumeric(qqnum)){
					chat.setQqnum(qqnum);
					chat.setTime(DataUtils.parseDate(gettime, "yyyy-MM-dd HH:mm:ss"));
					
					int lastBlankPos = line.lastIndexOf(" ");
					String nickName = qqnum;
					if(lastBlankPos+1 < pos)	//没有名字
						nickName = line.substring(lastBlankPos+1,pos-1);
					
					chat.setNickname(nickName);
					return chat;
				}
					
			}
		}
		return null;
	}
}
